package com.googlecode.zh.wicket;

import com.googlecode.zh.wicket.mask.MaskBehavior;
import com.googlecode.zh.wicket.mask.MaskedTextField;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HomePage extends WebPage {

    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = LoggerFactory.getLogger(HomePage.class);

    public HomePage(final PageParameters parameters) {
        super(parameters);
        add(new Label("version", getApplication().getFrameworkSettings().getVersion()));
        Form form = new Form("form");
        form.add(new FeedbackPanel("feedback").setOutputMarkupId(true));
        form.add(new MaskedTextField("maskField", new Model<String>("111"), "#9-9#-9999-#-##"));
        form.add(new CheckButton("maskTestButton", "maskField"));
        add(form);
        add(new MaskBehavior());
    }

    private static class CheckButton extends AjaxButton {

        private String checkId;

        public CheckButton(String id, String checkId) {
            super(id);
            this.checkId = checkId;
        }

        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            target.appendJavaScript("alert('" + checkId + " value: " + getParent().get(checkId).getDefaultModel().getObject() + "')");
        }

        protected void onError(AjaxRequestTarget target, Form<?> form) {
            target.add(getParent().get("feedback"));
        }
    }
}
